package project.main;

import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import project.crawler.downloader.PageDownloader;
import project.dbaccesslayer.MySQLAccessLayer;
import project.dbaccesslayer.Table_Questions_To_Download;

public class DownloadQuestionList {

	public static void main(String[] args) throws IOException, SQLException {
		if (args.length != 2) {
			System.out
					.println("Usage DownloadQuestionList <fromPage> <toPage>");
			return;
		}

		int startPage = Integer.parseInt(args[0]);
		int endPage = Integer.parseInt(args[1]);

		Connection conn = MySQLAccessLayer.openConnectionToMySql();

		for (int p = startPage; p <= endPage; p++) {
			String url = "http://stackoverflow.com/questions?"
					+ "pagesize=50&sort=votes&page=" + p;

			String html = PageDownloader.downloadUrl(url);

			List<Long> ids = getQuestionIDs(html);

			System.out.println("Got " + ids.size() + " ids.");

			Table_Questions_To_Download.saveQuestionsIds(conn, ids);
		}

		conn.close();
	}

	private static List<Long> getQuestionIDs(String html) {
		Pattern patt = Pattern.compile("\\/questions\\/(\\d+)\\/",
				Pattern.MULTILINE);
		Matcher m = patt.matcher(html);

		List<Long> ids = new ArrayList<Long>();
		while (m.find()) {
			ids.add(Long.parseLong(m.group(1)));
		}

		return ids;
	}

}
